# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gnutls
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=3.7.0
pkgver=${_pkgver}
pkgrel=2
pkgdesc="A library which provides a secure layer over a reliable transport layer (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL3' 'LGPL2.1')
url="https://www.gnutls.org/"
options=('!zipman')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libtasn1"
         "${MINGW_PACKAGE_PREFIX}-nettle>=3.6"
         "${MINGW_PACKAGE_PREFIX}-p11-kit>=0.23.1"
         "${MINGW_PACKAGE_PREFIX}-libunistring"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-unbound"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
# Need remove "autogen" package before building.
source=(https://www.gnupg.org/ftp/gcrypt/gnutls/v${_pkgver%.*}/${_realname}-${pkgver}.tar.xz{,.sig}
        0001-add-missing-define.patch
        0003-gnutls-fix-external-libtasn1-detection.patch
        0004-disable-broken-examples.patch
        0005-remove-coverage-rules.patch)
sha256sums=('49e2a22691d252c9f24a9829b293a8f359095bc5a818351f05f1c0a5188a1df8'
            'SKIP'
            'fe1d9df924f72ec82972df328467591e49fe82feacebaf2605d37cc059c5429e'
            '6493f69e782d60fe04de4b0040987e99851c522d0baf2fe25d10b85b63e97863'
            'cbf9e59fb2432d4ac1edcb70734023e074b8058fa4c010a036a40de8449f67fb'
            '10ea5a84459af8d848788cc8871decd3b3cdd542b829883ebe4f6ec28d60578f')
validpgpkeys=('0424D4EE81A0E3D119C6F835EDA21E94B565716F')  # "Simon Josefsson <simon@josefsson.org>"
validpgpkeys+=('1F42418905D8206AA754CCDC29EE58B996865171') # "Nikos Mavrogiannopoulos <nmav@gnutls.org>
validpgpkeys+=('462225C3B46F34879FC8496CD605848ED7E69871') # "Daiki Ueno <ueno@unixuser.org>"

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  patch -p1 -i ${srcdir}/0001-add-missing-define.patch
  patch -p1 -i ${srcdir}/0003-gnutls-fix-external-libtasn1-detection.patch
  patch -p1 -i ${srcdir}/0004-disable-broken-examples.patch
  patch -p1 -i ${srcdir}/0005-remove-coverage-rules.patch

  WANT_AUTOMAKE=latest autoreconf -fiv -I m4
}

build() {
  # Workaround for localtime_r functions
  CFLAGS+=" -D_POSIX_C_SOURCE"
  CXXFLAGS+=" -D_POSIX_C_SOURCE"

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${_pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --enable-cxx \
    --enable-nls \
    --disable-rpath \
    --disable-gtk-doc \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-libregex-libs=-lsystre \
    --enable-local-libopts \
    --disable-guile \
    --disable-libdane \
    --disable-tests \
    gl_cv_double_slash_root=yes

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -k check || /bin/true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
